package com.hjt.adb.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * 〈功能概述〉<br>
 *
 * @className: ShellUtil
 * @package: com.java.auto.utils
 * @author: zhangRenHuo
 * @date: 2020/8/5 10:06
 */
public class ShellUtil {
    public static Process adbCmd(String command) {
        //mac请使用adb的绝对路径
        return process("adb " + command);
    }

//    public static Process shell(String command) {
//        //mac请使用adb的绝对路径
//        return process("adb shell " + command);
////		return process(command);
//    }

    public static BufferedReader shellOut(Process ps) {
        BufferedInputStream in = new BufferedInputStream(ps.getInputStream());
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        return br;
    }
    
    public static void listenShellOut(Process ps, Function<String, Boolean> receiveLine) {
        BufferedReader br = shellOut(ps);
        String line;
        try {
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 输出adb 命令返回结果
    public static String getShellOut(Process ps) {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = shellOut(ps);
        String line;

        try {
            while ((line = br.readLine()) != null) {
                sb.append(line + System.getProperty("line.separator"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            return new String(sb.toString().trim().getBytes(), "gbk");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    // 输出adb 命令返回结果
    public static List<String> readShellOut(Process ps) {
        BufferedReader br = shellOut(ps);
        String line;
        List<String> result = new ArrayList<>();
        try {
            while ((line = br.readLine()) != null) {
            	result.add(new String(line.toString().trim().getBytes(), "gbk"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    private static Process process(String command) {
        Process ps = null;
        try {
            ps = Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ps;
    }

    public static void main(String[] args) {
        Process ps = null;
        try {
            ps = Runtime.getRuntime().exec("adb -s emulator-5564 shell input swipe  845 560 845 645 1000");
            while (ps.isAlive()) {
                try {
                    Thread.sleep(995);
                    ps.destroy();
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            Runtime.getRuntime().exec("adb -s emulator-5564 shell input tap  663 390");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
